//
//  XJTagSmAllCategoryViewController.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/6/25.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  Sm: supermarket

#import "XJTagSmAllCategoryViewController.h"
#import "RBGoodsDetailViewController.h" // 6-25跳转商品详情vc

//view
#import "XJAllCategoryCell.h"

//model
#import "XJAllCategoryModel.h"



@interface XJTagSmAllCategoryViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *clv;
@property (nonatomic, strong) NSMutableArray *dataArray;


@end

@implementation XJTagSmAllCategoryViewController

#pragma mark - 一 lazy
- (UICollectionView *)clv {
    if (!_clv) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        
        _clv = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _clv.backgroundColor = [UIColor whiteColor]; //不给颜色默认是黑的。

        _clv.delegate = self;
        _clv.dataSource = self;
        
        [_clv registerClass:[XJAllCategoryCell class] forCellWithReuseIdentifier:@"XJAllCategoryCell"];
    }
    
    return _clv;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


#pragma mark - 二 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //要减去导航的高度:为什么?
//    self.clv.frame = CGRectMake(0, 40, KWidth, KHeight - kStatusBarAndNavigationBarHeight - 40);
    
    self.clv.frame = CGRectMake(0, 0, KWidth, self.view.bounds.size.height - 40-kStatusBarAndNavigationBarHeight);
    [self.view addSubview:self.clv];
    
    
    [self requestCategoryDetailData];
}

#pragma mark - 三 UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XJAllCategoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"XJAllCategoryCell" forIndexPath:indexPath];
    
    XJAllCategoryModel *m = self.dataArray[indexPath.row];
    
    cell.model = m;
    
    return cell;
}

#pragma mark - item宽高 sbXcode 跳不进去
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((KWidth - 4 )/2, UI_IS_IPHONE5 ? ADAPTHeight(280) : ADAPTHeight(260));
}

#pragma mark - X间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 1;
}
#pragma mark - Y间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 1;
}

#pragma mark -  UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    XJLog(@"section:%ld--row:%ld",indexPath.section,indexPath.row);
    
    XJAllCategoryModel *model = self.dataArray[indexPath.row];
    
    RBGoodsDetailViewController *goodsDetailVC = [[RBGoodsDetailViewController alloc] init];
    goodsDetailVC.goodsId = model.goodsId;
    [self.navigationController pushViewController:goodsDetailVC animated:YES];
}


#pragma mark - 四 网络请求
//请求分类详细数据
- (void)requestCategoryDetailData {
    
    if ([NSString isEmptyString:self.category_id]) {
        XJLog(@"category_id 为空操作无效");
        return;
    }
    
    
    //模拟器不能定位。确保模拟器也要数据，so 写个死的。
    NSString *lat = @"30.194913";
    NSString *lng = @"120.189538";
    if ([NSString isEmptyString:self.lat]) {
        lat = @"30.194913";
    }else {
        lat = self.lat;
    }
    
    if ([NSString isEmptyString:self.lng]) {
        
        lng = @"120.189538";
    }else {
        lng = self.lng;
    }
    
//    @{@"lat":@"30.194913",
//      @"lng":@"120.189538",
//      @"page":@"1",
//      @"page_size":@"12",
//      @"category_id":@"12"
//      };
    NSDictionary *parDic = @{@"lat":lat,
                             @"lng":lng,
                             @"page":@"1",
                             @"page_size":@"12",
                             @"category_id":self.category_id
                             };
    
    [XJHttpTool requestShopCategoryDic:parDic successHandler:^(id responseObjc) {
      
        NSString *code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
        if ([code isEqualToString:@"0"]) {
            NSArray *category_goods_list = responseObjc[@"data"][@"category_goods_list"];
            
            for (NSDictionary *dic in category_goods_list) {
                XJAllCategoryModel *model = [XJAllCategoryModel mj_objectWithKeyValues:dic];
                
                [self.dataArray addObject:model];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.clv reloadData];
            });
        }
        
    } failHandler:^(NSError *error) {
        [JRToast showWithText:[error localizedDescription]];
    }];
}

@end
